<template>
  <div>
    <div class="export_box">
      <el-button type="primary" @click="visible = true">新增</el-button>
    </div>
    <el-table :data="tableData" style="width: 100%" :height="h" border>
      <el-table-column prop="company" label="所属公司" align="center">
      </el-table-column>
      <el-table-column prop="type" label="账户类型" align="center">
        <template slot-scope="scope">
          {{ scope.row.type == 1 ? "收入账户" : "支出账户" }}
        </template>
      </el-table-column>
      <el-table-column prop="name" label="账户名称" align="center">
      </el-table-column>
      <el-table-column prop="person" label="负责人" align="center">
      </el-table-column>
      <el-table-column prop="balance" label="账户余额" align="center">
      </el-table-column>
      <el-table-column prop="initial" label="账户初始资金" align="center">
      </el-table-column>
      <el-table-column prop="info" label="账号" align="center">
      </el-table-column>
      <el-table-column prop="remark" label="备注" align="center">
      </el-table-column>
      <el-table-column label="操作" align="center" width="120">
        <template slot-scope="scope">
          <!-- <el-button type="text" @click="showDetails(scope.row)"
            >变更明细</el-button
          > -->
          <el-button type="text" @click="editData(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- <div class="page_table">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="1"
        :page-sizes="[10, 20, 30]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pag.total"
      >
      </el-pagination>
    </div> -->
    <!-- 录入 -->
    <el-dialog
      title="单位信息录入"
      :visible.sync="visible"
      width="800px"
      :before-close="handleClose"
    >
      <el-form
        :model="form"
        :rules="rules"
        ref="ruleForm"
        :inline="true"
        label-width="150px"
      >
        <el-form-item label="账户类型" prop="type">
          <el-select v-model="form.type" :disabled="edit">
            <el-option value="1" label="收入账户"></el-option>
            <el-option value="2" label="支出账户"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属公司" prop="name">
          <el-select v-model="form.company_id" @change="changeCompany">
            <el-option
              v-for="item in list"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="账户名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="负责人" prop="person">
          <el-input v-model="form.person"></el-input>
        </el-form-item>
        <el-form-item label="账户初始资金" prop="initial">
          <el-input
            v-model="form.initial"
            type="number"
            :disabled="edit"
            @input="changeInitial"
          >
            <template slot="append">元</template></el-input
          >
        </el-form-item>
        <el-form-item label="账户余额" prop="balance">
          <el-input v-model="form.balance" type="number" disabled>
            <template slot="append">元</template></el-input
          >
        </el-form-item>
        <el-form-item label="账号" prop="info">
          <el-input
            v-model="form.info"
            type="textarea"
            :rows="2"
            :disabled="edit"
          ></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" :rows="3"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="submitAdd">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getDataByKey, openDB, changeDB, deleteDB, editDB } from "@/db/indexdb";
export default {
  data() {
    return {
      visible: false,
      form: {
        initial: "",
        balance: "",
      },
      rules: {
        type: [{ required: true, message: "请输入账户类型", trigger: "blur" }],
        name: [{ required: true, message: "请输入账户名称", trigger: "blur" }],
        info: [{ required: true, message: "请输入账户详情", trigger: "blur" }],
        person: [{ required: true, message: "请输入负责人", trigger: "blur" }],
        initial: [
          { required: true, message: "请输入初始金额", trigger: "blur" },
        ],
      },

      tableData: [],
      edit: false,
      list: [],
      h: "",
    };
  },
  created() {
    openDB("car", "list").then(() => {
      this.getList();
      this.getOther();
    });
    this.h = document.documentElement.offsetHeight - 270;
  },
  methods: {
    getList() {
      getDataByKey("list", "账户表").then((res) => {
        res?.data.sort((b, a) => {
          return Number(a.id) - Number(b.id);
        });
        this.tableData = res?.data;
      });
    },
    getOther() {
      getDataByKey("list", "公司信息表").then((res) => {
        res?.data.sort((b, a) => {
          return Number(a.id) - Number(b.id);
        });
        this.list = res?.data;
      });
    },
    changeCompany(val) {
      let result =
        this.list.filter((item) => {
          return item.id == val;
        })[0].name || "";
      this.form.company = result;
    },
    submitAdd() {
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          let data = { ...this.form };
          if (this.edit) {
            editDB("list", "账户表", data).then((res) => {
              this.getList();
            });
          } else {
            data.id = new Date().getTime();
            changeDB("list", "账户表", data).then(() => {
              this.getList();
            });
          }
          this.handleClose();
        } else {
          return false;
        }
      });
    },
    handleClose() {
      this.edit = false;
      this.visible = false;
      this.form = { initial: "", balance: "" };
    },
    changeInitial() {
      this.form.balance = this.form.initial;
    },
    editData(row) {
      this.edit = true;
      this.visible = true;
      this.form = { ...row };
    },
    handleSizeChange(val) {
      this.pag.size = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.pag1.page = val;
      this.getList();
    },
    showDetails() {
      // console.log("123123");
    },
  },
};
</script>

<style lang="scss" scoped>
.search_box {
  display: flex;
  flex-wrap: wrap;
  border-bottom: 1px solid #ccc;
  .el-input,
  .el-select {
    width: 200px;
    margin-right: 20px;
    margin-bottom: 20px;
  }
  .el-date-editor {
    margin-right: 20px;
    margin-bottom: 20px;
  }
  .el-button {
    margin-bottom: 20px;
  }
}
.export_box {
  display: flex;
  justify-content: flex-end;
  margin: 10px 0;
}
.page_table {
  margin-top: 20px;
  width: 100%;
  display: flex;
  justify-content: flex-end;
}
.el-form {
  .el-select,
  .el-input,
  .el-date-editor {
    width: 200px;
  }
  .el-textarea {
    width: 560px;
  }
  .el-range-editor {
    width: 290px;
  }
}
.pro_box {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 15px;
}
</style>
